package com.sgb.kjwl.viewmodel.manage;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.manage.SpecialQualificationEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.ManageRepertory;
import com.sgb.kjwl.view.ui.activity.manage.SpecialQualificationActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SpecialQualificationModel1 extends BaseViewModel {
    private SpecialQualificationActivity activity;
    private ManageRepertory repertory;
    private int pageNum;

    public SpecialQualificationModel1(SpecialQualificationActivity specialQualificationActivity, ManageRepertory repertory) {
        this.activity = specialQualificationActivity;
        this.repertory = repertory;
    }

    //资质类型
    public void findMyListByCompanyInfoNo() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getQualifyTypeTreeList().enqueue(new Callback<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> call, Response<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(activity, "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<ParentAreaEntity<List<ParentAreaEntity>>> list = new ArrayList<>();
                    list.addAll(response.body().getData());
                    ParentAreaEntity entity = new ParentAreaEntity();
                    entity.setNo("");
                    entity.setName("不限");
                    list.add(0, entity);
                    activity.showSpecialTypeWindow(list);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    //筛选类型
    public void findMyListByCompanyInfoNo(Map<String, Object> map, boolean isLodMore) {
        pageNum = isLodMore ? pageNum + 1 : 1;
        //limit
        map.put("limit", 20);
        //page
        map.put("page", pageNum);
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().findMyListByCompanyInfoNo(map).enqueue(new Callback<BaseEntity<ListResponse<List<SpecialQualificationEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<SpecialQualificationEntity>>>> call, Response<BaseEntity<ListResponse<List<SpecialQualificationEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null || response.body().getData().getList() == null) {
                    MToast.showToast(activity, "暂无数据");
                    activity.showList(new ArrayList<>(), isLodMore);
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showList(response.body().getData().getList(), isLodMore);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<SpecialQualificationEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void queryIndustryCategry(boolean isList) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity<List<PickSelectEntity>>> list = new ArrayList<>();
                    list.add(new PickSelectEntity("","全部"));
                    list.addAll(response.body().getData());
                    activity.tabData(list,isList);
                } else {
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getDepartment() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getDepartment().enqueue(new Callback<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> call, Response<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<ParentAreaEntity<List<ParentAreaEntity>>> list = new ArrayList<>();
                    list.addAll(response.body().getData());
                    ParentAreaEntity entity2 = new ParentAreaEntity("", "不限", "");
                    list.add(0, entity2);
                    activity.setDepartmentList(list);
                } else {
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getCityData() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setCityData(response.body().getData(),repertory.getAreaCode());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
